<?php 
/**
* 简历表1模型
*/
class Resume1Model extends Model
{
	public $table = 'people_resume1';

	/**
	 * [$validate 自动验证]
	 * @var array
	 */
	public $validate = array(

		array('resumename','nonull','简历名称不能为空',2,3),
		array('resumename','maxlen:20','简历名称不能超过20个字符',2,3),
		array('validtime','num:0,365','有效时间必须为数字且不能超过365天',2,3),
		array('realname','nonull','真实姓名不能为空',2,3),
		array('sex','num:1,2','性别选择错误',2,3),
		array('age','num:18,80','年龄必须是18岁以上80岁以下',2,3),
		array('height','num:50,220','身高必须为数字且为50-220CM之间',3,3),
		array('marrystate','num:1,3','婚姻状态选择错误',2,3),
		array('workyear','num:1,6','请选择工作经验',2,3),
		array('household','nonull','户口所在地不能为空',2,3),
		array('household','maxlen:150','户口所在地不能超过150个字符',2,3),
		array('edu','num:1,9','请选择学历',2,3),
		array('phonenumber','phone','联系手机格式不正确',2,3),
		array('mail','email','电子邮箱格式不正确',2,3),
		array('address','nonull','通讯地址不能为空',2,3),
		array('address','maxlen:150','通讯地址不能超过150个字符',2,3),
		array('qq','regexp:/^[1-9]\d{4,10}$/','qq号码格式不正确',3,3),
		array('homepage','http','个人主页/博客格式不正确',3,3)
		);

	/**
	 * [$auto 自动完成]
	 * @var [type]
	 */
	public $auto = array(
		array('user_uid','_uid','method',2,1),
		array('sendtime','_sendtime','method',2,1),
		array('endtime','_endtime','method',2,3),
		array('freshtime','_freshtime','method',2,1),
		array('validtime','_validtime','method',2,3)
		); 
	/**
	 * 获取用户uid
	 * @return [type] [description]
	 */
	public function _uid()
	{
		return session('uid');
	}

	/**
	 * 简历的发表时间
	 * @return [type] [description]
	 */
	public function _sendtime()
	{
		return time();
	}

	/**
	 * 结束时间
	 * @return [type] [description]
	 */
	public function _endtime()
	{
		// 有效天数
		$validtime = Q('post.validtime',0,'intval');
		//如果有reid提交证明是修改
		if($reid = Q('post.reid', 0, 'intval')){
			//查询旧数据
			$data = $this->where(array('reid'=>$reid))->field('validtime,sendtime')->find();
			$ovalidtime = $data['validtime'];
			$sendtime = $data['sendtime'];

			//结束时间(时间戳)
			return $sendtime + ( ($ovalidtime + $validtime) * 3600 * 24 );

		}
		return time() + $validtime * 24 * 3600;
	}

	/**
	 * 刷新时间
	 * @return [type] [description]
	 */
	public function _freshtime()
	{
		return time();
	}

	/**
	 * 有效时间
	 * @return 	 	
	 */
	public function _validtime()
	{	
		$validtime = Q('post.validtime',0,'intval');
		//如果为修改则是旧时间加上提交过来的延长时间
		if($reid = Q('post.reid',0,'intval')){
			$ovalidtime = $this->where(array('reid'=>$reid))->getField('validtime');
			return $validtime + $ovalidtime;
		}
		return $validtime;
	}

	/**
	 * 添加数据
	 */
	public function add_data()
	{
		$where = array(
				'user_uid'=>session('uid')
			);
		if($this->where($where)->count()>3){
			$this->error='超过三条简历';
			return false;
		}
		$reid  = $this->add();
		$this->add_step($reid,1);
		return $reid;
	}

	/**
	 * 添加步骤
	 * @param [type] $reid [description]
	 * @param [type] $step [description]
	 */
	public function add_step($reid,$step)
	{
		$where = array('reid' => $reid);

		#取出现有的状态
		$stepStatus = $this->where($where)->getField('stepstatus');
		// p($stepStatus);exit;
		$step = strval($step);
		if(!strpos($stepStatus, $step)) $stepStatus .= $step . ',';
		return $this->where($where)->save(array('stepstatus' => $stepStatus));
	}

	/**
	 * 检查是否有信息
	 * @param  [type] $reid [description]
	 * @return [type]       [description]
	 */
	public function have_info($reid)
	{
		$where = array(
			'reid' => $reid,
			'user_uid' => session('uid')
			);
		return $this->where($where)->getField('reid');
	}

	/**
	 * 检查是否是当前用户在操作
	 * @param  [type] $reid [description]
	 * @return [type]       [description]
	 */
	public function check_user($reid)
	{
		$user_uid = $this->where('reid = '.$reid)->getField('user_uid');

		if ($user_uid != session('uid')) {
			halt('非法操作');
		}
		return $user_uid;
	}

	/**
	 * 获取信息
	 * @param  [type] $reid [description]
	 * @return [type]       [description]
	 */
	public function get_info($reid)
	{
		$where = array('reid'=>$reid);
		$res = $this->where($where)->find();
		if($res['height'] == 0) unset($res['height']);
		return $res;
	}


	public function update_data($where=null,$data=null)
	{
		
		return $this->where($where)->save($data);
	}


	/**
	 * 删减步骤
	 * @param  [type] $reid [description]
	 * @param  [type] $step [description]
	 * @return [type]       [description]
	 */
	public function minus_step($reid,$step){
		$where = array('reid'=>$reid);
		//拿出以前的状态
		$stepstatus = $this->where($where)->getField('stepstatus');
		if(!preg_match("/$step/", $stepStat)) return false;

		$stepStat = preg_replace("/$step,/", '', $stepstatus);

		$this->where($where)->save(array('stepstatus'=>$stepstatus));

		return true;

	}


	public function get_step($reid)
	{
		$where = array('reid'=>$reid);
		//拿出以前的状态
		$stepstatus = $this->where($where)->getField('stepstatus');
		if(!$stepstatus) return false;
		return explode(',', rtrim($stepstatus,','));
	}


	public function get_progress($reid)
	{
		$stepstatus = $this->get_step($reid);
		if(!$stepstatus) return 0;
		// p($stepstatus);
		$progress = 0;
		foreach ($stepstatus as $v) {
			if($v == 6){
				$progress += 12;
			}else if($v == 7){
				$progress += 13;
			}else{
				$progress += 15;
			}
		}
		return $progress;
	}

	public function get_resume($uid = NULL){

		$where = array();
		//如有有用户uid则处理当前用户，如果没有则不限制用户为后台管理准备
		if($uid) $where['user_uid'] = $uid;

		//通过get参数形成不同的情况组合不同的where
		switch (Q('get.state', 0, 'intval')) {
			case 1:
				$state = 1;
				break;
			case 2:
				$state = 2;
				break;
			case 3:
				$state = 3;
				break;
			
			default:
				$_GET['state'] = 0;
				$state = NULL;
				break;
		}

		if($state) $where['verifystate'] = $state;
		$data = $this->where($where)->all();
		if(!$data) return false;


		//处理格式化数据
		foreach ($data as $k => $v) {
			$data[$k]['verify'] = $v['verifystate'];
			switch ($v['verifystate']) {
				case 1:
					$data[$k]['verifystate'] = '审核中';
					break;
				case 2:
					$data[$k]['verifystate'] = '审核通过';
					break;
				
				case 3:
					$data[$k]['verifystate'] = '审核失败';
					break;
			}

			$data[$k]['endtime'] = date('Y-m-d',$v['endtime']);
			$data[$k]['freshtime'] = date('Y-m-d',$v['freshtime']);

			$data[$k]['progress'] = $this->get_progress($v['reid']);
		}


		return $data;

	}


	/**
	 * 包括刷新简历与删除简历
	 * @return [type] [description]
	 */
	public function resume_action(){
		$reidArr = Q('post.reid');
		if(!$reidArr) return false;

		//提升排名
		if(Q('post.up')){
			foreach ($reidArr as $v) {
				$where = array(
					'reid'		=>	$v,
					'user_uid'	=>	session('uid')
					);
				$this->where($where)->save(array('freshtime'=>time()));
			}
			return true;
		}

		//如果是删除简历
		if(Q('post.delete')){
			foreach ($reidArr as $v) {
				K('ResumeRelation')->del_resume($v,session('uid'));
			}
			return true;
		}

		
	}

	/**
	 * 修改隐私状态
	 * @return [type] [description]
	 */
	public function update_status(){
		$reid = Q('post.reid', 0, 'intval');
		//检测是否是当前用户
		$this->check_user($reid);

		$where = array(
			'user_uid'	=>	session('uid'),
			'reid'		=>	$reid
			);
		//修改第一张表状态
		$this->where($where)->save();

		$facepublic = Q('post.facepublic', NULL, 'intval');
		if(!is_null($facepublic)){
			K('Resume7')->where(array('resume1_reid'=>$reid))->save();
		}
		

	}

	 /**
     * 提升所有简历排名
     * @param  [type] $reid [第一阶段简历主键id,默认为NULL
     * 用户传递reid则修改单条刷新时间，如果不传则修改所有简历刷新时间]
     * @return [type]       [description]
     */
    public function refresh_resume($reid = NULL){
        //定义where条件，保证当前用户
        $where = array('user_uid' => session('uid'));
        //如果用户传递reid则修改单条刷新时间，如果不传则修改所有简历刷新时间
        if(!$reid) $where['reid'] = $reid;
        //通过用户uid更改当前用户简历刷新时间
        $this->where($where)->save(array('freshtime'=>time()));

    }

    /**
     * 检测简历状态
     * 1.用户是否发表过简历
     * 2.简历审核是否通过
     * 3.简历是否过期
     * @return [type] [description]
     */
    public function check_resume(){
    	$data = $this->where(array('user_uid'=>session('uid')))->all();
    	if(!$data) return false;

    	//简历是否通过标识变量，默认为假，只要一条通过，则改为真
    	$pass = false;

    	foreach ($data as $v) {
    		if($v['verifystate'] == 2 && $v['endtime'] > time()){
    			$pass = true;
    		}
    	}

    	//如果$pass为真，证明至少一条是通过的
    	return $pass;
    }

    /**
     * 当前用户通过的简历
     * @return [type] [description]
     */
    public function get_pass_resume(){
    	$where = array(
    		'verifystate'	=>	2,
    		'endtime'		=>	array('gt'=>time()),
    		'user_uid'		=>	session('uid')
    		);
    	return $this->where($where)->all();
    }


    /**
     * 改变审核状态
     * @return [type] [description]
     */
    public function change_verify($where,$verify){
    	$this->where($where)->save(array('verifystate'=>$verify));
    }
	
}

 ?>